from datetime import datetime, timedelta
import os
import pandas as pd
from utils.get_work_number import get_work_number
from utils.datetime_tools import get_days_to_week_day, get_days_of_month
from utils.generate_data import normal_day_data_generate, other_day_data_generate
from utils.generage_leave_data import get_leave_data


# name_file, original_data, leave_file, holidays_file, rest_day, year, month
def main(name_file, original_data, leave_file, holidays_file, rest_day, year, month):
    work_numbers = get_work_number(name_file) #  获取工号用于筛选数据

    original_data_df = pd.read_excel(original_data)
    selected_df = original_data_df.iloc[:, [0, 1, 2, 3, 4, 5]].copy()
    selected_df = selected_df[selected_df.iloc[:, 0].isin(work_numbers)]

    new_col = 'X' + selected_df.iloc[:, 0].astype(str).str[1:]
    selected_df = selected_df.assign(**{selected_df.columns[0]: new_col})
    original_table = selected_df.values

    holidays_table = pd.read_excel(holidays_file)
    holidays_table = holidays_table.iloc[:, [0, 1]]



    dataframe_columns = ["工号", "姓名", "刷入日期", "刷入时间", "刷出日期", "刷出时间", "班制", "班别", "星期"]
    dataframe_index = None
    dataframe_data = []

    for i in range(1, int(len(original_table) / get_days_of_month(year, month) + 1)):
        for j in range(get_days_of_month(year, month) * (i - 1), get_days_of_month(year, month) * i):

            type_of_work = original_table[j][5]

            if original_table[j][2] in (holidays_table[holidays_table.iloc[:, 1].isin([type_of_work])]).iloc[:, 0].tolist():
                dataframe_data.append([
                    original_table[j][0],
                    original_table[j][1],
                    original_table[j][2].date(),
                    "None",
                    original_table[j][2].date(),
                    "None",
                    type_of_work,
                    "休息日",
                    original_table[j][3]
                ])

                continue

            elif type_of_work == "常日班":
                normal_day_data = normal_day_data_generate(original_table[j][4])

                dataframe_data.append([
                    original_table[j][0],
                    original_table[j][1],
                    original_table[j][2].date(),
                    normal_day_data[0],
                    original_table[j][2].date(),
                    normal_day_data[1],
                    type_of_work,
                    original_table[j][4],
                    original_table[j][3]
                ])

            elif type_of_work == "其他":
                if rest_day == "周六" and original_table[j][3] == "日":
                    try:
                        last_monday_work_time = original_table[j + 1][4] if original_table[j + 1][4] != "夜班" else original_table[j + 1][4]
                        other_day_data = other_day_data_generate(last_monday_work_time)

                        if last_monday_work_time == "夜班":
                            dataframe_data.append([
                                original_table[j][0],
                                original_table[j][1],
                                original_table[j][2].date(),
                                other_day_data[0],
                                original_table[j][2].date() + timedelta(days=1),
                                other_day_data[1],
                                type_of_work,
                                last_monday_work_time,
                                original_table[j][3]
                            ])
                        else:
                            dataframe_data.append([
                                original_table[j][0],
                                original_table[j][1],
                                original_table[j][2].date(),
                                other_day_data[0],
                                original_table[j][2].date(),
                                other_day_data[1],
                                type_of_work,
                                "休息日" if last_monday_work_time == "常日班" else last_monday_work_time,
                                original_table[j][3]
                            ])
                    except:
                        last_monday_work_time = original_table[j + 1 - 7][4]
                        other_day_data = other_day_data_generate(last_monday_work_time)

                        if last_monday_work_time == "夜班":
                            dataframe_data.append([
                                original_table[j][0],
                                original_table[j][1],
                                original_table[j][2].date(),
                                other_day_data[0],
                                original_table[j][2].date() + timedelta(days=1),
                                other_day_data[1],
                                type_of_work,
                                last_monday_work_time,
                                original_table[j][3]
                            ])
                        else:
                            dataframe_data.append([
                                original_table[j][0],
                                original_table[j][1],
                                original_table[j][2].date(),
                                other_day_data[0],
                                original_table[j][2].date(),
                                other_day_data[1],
                                type_of_work,
                                "休息日" if last_monday_work_time == "常日班" else last_monday_work_time,
                                original_table[j][3]
                            ])

                elif rest_day == "周日" and original_table[j][3] == "六":
                    try:
                        last_friday_work_time = original_table[j - 1][4]
                        other_day_data = other_day_data_generate(last_friday_work_time)

                        if last_friday_work_time == "夜班":
                            dataframe_data.append([
                                original_table[j][0],
                                original_table[j][1],
                                original_table[j][2].date(),
                                other_day_data[0],
                                original_table[j][2].date() + timedelta(days=1),
                                other_day_data[1],
                                type_of_work,
                                last_friday_work_time,
                                original_table[j][3]
                            ])
                        else:
                            dataframe_data.append([
                                original_table[j][0],
                                original_table[j][1],
                                original_table[j][2].date(),
                                other_day_data[0],
                                original_table[j][2].date(),
                                other_day_data[1],
                                type_of_work,
                                "休息日" if last_friday_work_time == "常日班" else last_friday_work_time,
                                original_table[j][3]
                            ])
                    except:
                        last_friday_work_time = original_table[j - 1 + 7][4]
                        other_day_data = other_day_data_generate(last_friday_work_time)

                        if last_friday_work_time == "夜班":
                            dataframe_data.append([
                                original_table[j][0],
                                original_table[j][1],
                                original_table[j][2].date(),
                                other_day_data[0],
                                original_table[j][2].date() + timedelta(days=1),
                                other_day_data[1],
                                type_of_work,
                                last_friday_work_time,
                                original_table[j][3]
                            ])
                        else:
                            dataframe_data.append([
                                original_table[j][0],
                                original_table[j][1],
                                original_table[j][2].date(),
                                other_day_data[0],
                                original_table[j][2].date(),
                                other_day_data[1],
                                type_of_work,
                                "休息日" if last_friday_work_time == "常日班" else last_friday_work_time,
                                original_table[j][3]
                            ])

                else:
                    other_day_data = other_day_data_generate(original_table[j][4])

                    if original_table[j][4] == "夜班":
                        dataframe_data.append([
                            original_table[j][0],
                            original_table[j][1],
                            original_table[j][2].date(),
                            other_day_data[0],
                            original_table[j][2].date() + timedelta(days=1),
                            other_day_data[1],
                            type_of_work,
                            original_table[j][4],
                            original_table[j][3]
                        ])
                    else:
                        dataframe_data.append([
                            original_table[j][0],
                            original_table[j][1],
                            original_table[j][2].date(),
                            other_day_data[0],
                            original_table[j][2].date(),
                            other_day_data[1],
                            type_of_work,
                            original_table[j][4],
                            original_table[j][3]
                        ])

            else:
                sat_diff, sun_diff = get_days_to_week_day((original_table[j][2]).date())

                #  判断 休息日数据
                if rest_day == "周六" and original_table[j][3] == "六":
                    dataframe_data.append([
                        original_table[j][0],
                        original_table[j][1],
                        original_table[j][2].date(),
                        "None",
                        original_table[j][2].date(),
                        "None",
                        type_of_work,
                        "休息日",
                        original_table[j][3]
                    ])
                    continue
                elif rest_day == "周日" and original_table[j][3] == "日":
                    dataframe_data.append([
                        original_table[j][0],
                        original_table[j][1],
                        original_table[j][2].date(),
                        "None",
                        original_table[j][2].date() + timedelta(days=1),
                        "None",
                        type_of_work,
                        "休息日",
                        original_table[j][3]
                    ])
                    continue

                try:
                    if original_table[j + sat_diff][4] != "休息日" and original_table[j + sat_diff][1] == original_table[j][1]:
                        second_other_day_data = other_day_data_generate(original_table[j + sat_diff][4])
                        if original_table[j + sat_diff][4] == "夜班":
                            dataframe_data.append([
                                original_table[j][0],
                                original_table[j][1],
                                original_table[j][2].date(),
                                second_other_day_data[0],
                                original_table[j][2].date() + timedelta(days=1),
                                second_other_day_data[1],
                                type_of_work,
                                original_table[j + sat_diff][4],
                                original_table[j][3],
                            ])
                        else:
                            dataframe_data.append([
                                original_table[j][0],
                                original_table[j][1],
                                original_table[j][2].date(),
                                second_other_day_data[0],
                                original_table[j][2].date(),
                                second_other_day_data[1],
                                type_of_work,
                                original_table[j + sat_diff][4],
                                original_table[j][3]
                            ])
                    elif original_table[j + sun_diff][4] != "休息日" and original_table[j + sun_diff][1] == original_table[j][1]:
                        second_other_day_data = other_day_data_generate(original_table[j + sun_diff][4])
                        if original_table[j + sun_diff][4] == "夜班":
                            dataframe_data.append([
                                original_table[j][0],
                                original_table[j][1],
                                original_table[j][2].date(),
                                second_other_day_data[0],
                                original_table[j][2].date() + timedelta(days=1),
                                second_other_day_data[1],
                                type_of_work,
                                original_table[j + sun_diff][4],
                                original_table[j][3],
                            ])
                        else:
                            dataframe_data.append([
                                original_table[j][0],
                                original_table[j][1],
                                original_table[j][2].date(),
                                second_other_day_data[0],
                                original_table[j][2].date(),
                                second_other_day_data[1],
                                type_of_work,
                                original_table[j + sun_diff][4],
                                original_table[j][3]
                            ])
                    else:
                        if original_table[j + sat_diff - 7][4] != "休息日":
                            second_other_day_data = other_day_data_generate(original_table[j + sat_diff - 7][4])
                            if original_table[j + sat_diff - 7][4] == "夜班":
                                dataframe_data.append([
                                    original_table[j][0],
                                    original_table[j][1],
                                    original_table[j][2].date(),
                                    second_other_day_data[0],
                                    original_table[j][2].date() + timedelta(days=1),
                                    second_other_day_data[1],
                                    type_of_work,
                                    original_table[j + sat_diff - 7][4],
                                    original_table[j][3],
                                ])
                            else:
                                dataframe_data.append([
                                    original_table[j][0],
                                    original_table[j][1],
                                    original_table[j][2].date(),
                                    second_other_day_data[0],
                                    original_table[j][2].date(),
                                    second_other_day_data[1],
                                    type_of_work,
                                    original_table[j + sat_diff - 7][4],
                                    original_table[j][3]
                                ])
                        elif original_table[j + sun_diff - 7][4] != "休息日":
                            second_other_day_data = other_day_data_generate(original_table[j + sun_diff - 7][4])
                            if original_table[j + sun_diff - 7][4] == "夜班":
                                dataframe_data.append([
                                    original_table[j][0],
                                    original_table[j][1],
                                    original_table[j][2].date(),
                                    second_other_day_data[0],
                                    original_table[j][2].date() + timedelta(days=1),
                                    second_other_day_data[1],
                                    type_of_work,
                                    original_table[j + sun_diff - 7][4],
                                    original_table[j][3],
                                ])
                            else:
                                dataframe_data.append([
                                    original_table[j][0],
                                    original_table[j][1],
                                    original_table[j][2].date(),
                                    second_other_day_data[0],
                                    original_table[j][2].date(),
                                    second_other_day_data[1],
                                    type_of_work,
                                    original_table[j + sun_diff - 7][4],
                                    original_table[j][3]
                                ])
                except:
                    if original_table[j + sat_diff - 7][4] != "休息日":
                        second_other_day_data = other_day_data_generate(original_table[j + sat_diff - 7][4])
                        if original_table[j + sat_diff - 7][4] == "夜班":
                            dataframe_data.append([
                                original_table[j][0],
                                original_table[j][1],
                                original_table[j][2].date(),
                                second_other_day_data[0],
                                original_table[j][2].date() + timedelta(days=1),
                                second_other_day_data[1],
                                type_of_work,
                                original_table[j + sat_diff - 7][4],
                                original_table[j][3]
                            ])
                        else:
                            dataframe_data.append([
                                original_table[j][0],
                                original_table[j][1],
                                original_table[j][2].date(),
                                second_other_day_data[0],
                                original_table[j][2].date(),
                                second_other_day_data[1],
                                type_of_work,
                                original_table[j + sat_diff - 7][4],
                                original_table[j][3]
                            ])
                    elif original_table[j + sun_diff - 7][4] != "休息日":
                        second_other_day_data = other_day_data_generate(original_table[j + sun_diff - 7][4])
                        if original_table[j + sun_diff - 7][4] == "夜班":
                            dataframe_data.append([
                                original_table[j][0],
                                original_table[j][1],
                                original_table[j][2].date(),
                                second_other_day_data[0],
                                original_table[j][2].date() + timedelta(days=1),
                                second_other_day_data[1],
                                type_of_work,
                                original_table[j + sun_diff - 7][4],
                                original_table[j][3]
                            ])
                        else:
                            dataframe_data.append([
                                original_table[j][0],
                                original_table[j][1],
                                original_table[j][2].date(),
                                second_other_day_data[0],
                                original_table[j][2].date(),
                                second_other_day_data[1],
                                type_of_work,
                                original_table[j + sat_diff - 7][4],
                                original_table[j][3]
                            ])



    new_dataframe = pd.DataFrame(dataframe_data, columns=dataframe_columns, index=dataframe_index)

    final_dataframe = get_leave_data(new_dataframe, work_numbers, leave_file)

    result_dir = "results"
    if not os.path.isdir(result_dir):
        os.mkdir(result_dir)
    final_dataframe.to_excel(f"{result_dir}/{year}-{month}.xlsx", index=dataframe_index, columns=dataframe_columns)

    print(final_dataframe)
